package com.jk.controller;

import com.jk.po.AreaPO;
import com.jk.po.DestinationPO;
import com.jk.po.HotelPO;
import com.jk.po.SharingPO;
import com.jk.service.DestinationService;
import com.jk.vo.AreaVO;
import com.jk.vo.CustomerVO;
import freemarker.template.Configuration;
import freemarker.template.Template;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("destination")
@Api("目的地")
public class DestinationController {
    //日志记录对象
    private static Logger logger = LoggerFactory.getLogger(TestController.class);

    @Autowired
    private DestinationService destinationService;
/*
 * @param dPO
 * @author 赵磊荣
 * @date 2021/6/30 19:22
 * @return 查询目的地信息
 */
    @GetMapping("queryDestination")
    @ApiOperation("查询所有目的地信息")
    public List<DestinationPO>queryDestination(){
        List<DestinationPO>list=null;
        try {
            list=destinationService.queryDestination();
            logger.info("查询目的地信息成功");
        }catch (Exception e){
            logger.error("查询目的地信息异常:"+e.getMessage());
        }
        return list;
    }
/*
 * @author 赵磊荣
 * @date 2021/7/1 15:27
 * @return 查询热度排序
 */
    @GetMapping("querydestinationheat")
    @ApiOperation("查询景区热度从高到底")
    public List<DestinationPO>querydestinationheat(){
      List<DestinationPO>list=null;
      try{
          list = destinationService.querydestinationheat();
          logger.info("查询景区热度成功");
      }catch (Exception e) {
          logger.error("查询景区热度失败" + e.getMessage());
      }
        return list;
    }

    /*
     * @author 赵磊荣
     * @date 2021/7/1 15:40
     * @return 查询等级排序
     */
    @GetMapping("querydestinationlevel")
    @ApiOperation("查询景区等级从高到底")
    public List<DestinationPO>querydestinationlevel(){
        List<DestinationPO>list = null;
        try{
            list = destinationService.querydestinationlevel();
            logger.info("查询景区等级成功");
        }catch (Exception e) {
            logger.error("查询景区等级失败" + e.getMessage());
        }
        return list;
    }



    /**
     * @author 段卓帆
     * @date 2021/7/1 15:20
     * @return 查询景区价格从低到高
     */
    @GetMapping("queryByPrice")
    @ApiOperation("查询景区价格从低到高")
    public List<DestinationPO> queryByPrice(){
        List<DestinationPO> list = null;
        try {
            list = destinationService.queryByPrice();
            logger.info("查询景区信息成功");
        }catch (Exception e){
            logger.error("查询景区信息异常:" + e.getMessage());
        }
        return list;
    }

    /**
     * @author 段卓帆
     * @date 2021/7/1 15:20
     * @return 查询景区价格从高到低
     */
    @GetMapping("queryByPriceDesc")
    @ApiOperation("查询景区价格从高到低")
    public List<DestinationPO> queryByPriceDesc(){
        List<DestinationPO> list = null;
        try {
            list = destinationService.queryByPriceDesc();
            logger.info("查询景区信息成功");
        }catch (Exception e){
            logger.error("查询景区信息异常:" + e.getMessage());
        }
        return list;
    }



    /**
     *
     *
     * @author 赵磊荣
     * @date 2021/7/5 19:38
     * @return 查询地区表
     */

    @GetMapping("queryAreaTemplate")
    @ApiOperation("静态模板自动生成地区信息")
    public @ResponseBody void queryAreaTemplate() throws IOException {
        //获取路径
        File file = new File("");
        String path =file.getCanonicalPath();
        //字符串拼接路径
        String filePath=path+"/"+"lvyou-consumer"+"/"+"src/"+"main/"+"resources/"+"static/"+"nav"+"/"+"destination"+"/";

        //取所有数据
        List<AreaVO> ArarList = destinationService.queryAreaList();


        FreeMarkerConfigurationFactoryBean freeMarkerConfigurationFactoryBean = new FreeMarkerConfigurationFactoryBean();
        freeMarkerConfigurationFactoryBean.setTemplateLoaderPath("classpath:templates");
        Configuration configuration = null;
        FileWriter fileWriter = null;

        try {
            try {
                configuration = freeMarkerConfigurationFactoryBean.createConfiguration();
                Template template = configuration.getTemplate("destination.ftl");
                fileWriter = new FileWriter(filePath+"destination"+".html");

                HashMap<String, Object> map = new HashMap<String, Object>();

                //所有数据
                map.put("area",ArarList);

                template.process(map,fileWriter);
            }catch (Exception e){
                e.printStackTrace();
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if ( fileWriter != null){
                try {
                    fileWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    /**
     * @author dzf
     * @date 2021/7/6 17:09
     * @return 静态模板自动生成目的地模板
     */

    @GetMapping("querymudidiTemplate/{taid}")
    @ApiOperation("静态模板自动生成目的地信息")
    public @ResponseBody void querymudidiTemplate(@PathVariable("taid")Integer taid) throws IOException {

        //获取路径
        File file = new File("");
        String path =file.getCanonicalPath();
        //字符串拼接路径
        String filePath=path+"/"+"lvyou-consumer"+"/"+"src/"+"main/"+"resources/"+"static/"+"nav"+"/"+"mudidi"+"/";

        //取地区和景点表的所有数据
        //List<AreaVO> ArarList = destinationService.queryAreaAndTAList();
        AreaVO areaVO = destinationService.queryAreaAndTAByTaid(taid);


        FreeMarkerConfigurationFactoryBean freeMarkerConfigurationFactoryBean = new FreeMarkerConfigurationFactoryBean();
        freeMarkerConfigurationFactoryBean.setTemplateLoaderPath("classpath:templates");
        Configuration configuration = null;
        FileWriter fileWriter = null;

        try {
            try {
                configuration = freeMarkerConfigurationFactoryBean.createConfiguration();
                Template template = configuration.getTemplate("mudidi.ftl");
                fileWriter = new FileWriter(filePath+"mudidi"+".html");

                HashMap<String, Object> map = new HashMap<String, Object>();

                //所有数据
                //map.put("areaTA",ArarList);
                map.put("aa",areaVO);

                template.process(map,fileWriter);
            }catch (Exception e){
                e.printStackTrace();
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if ( fileWriter != null){
                try {
                    fileWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


}
